const util = {
	/*
	 * 判断身份证
	 */
	card_validation:function(cardid){
		if(!(/^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/.test(cardid))) {
			mui.toast("身份证号有误，请重新输入");
			return false;
		} else {
			return true;
		}
	},
	/*
	 * 判断手机号
	 */
	phone_validation:function(phone) {
		if(!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
			mui.toast("手机号码有误，请重新输入");
			return false;
		} else {
			return true;
		}
	},
	/*
	 * 车牌判断
	 */
	plate_number_validation:function(str) {
		if(str.length == 7) {
			var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
			if(!express.test(str)) {
				mui.toast("车牌号有误，请重新输入");
			} else {
				return true;
			}
		} else {
			mui.toast("车牌号有误，请重新输入");
			return false;
		}
	},
	/*
	 * 判断是pc还是移动端
	 */
	gomatch:function() {
        if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
            return "mobile";
        }
        else {
            return "pc";
        }
                
    },
    /*
     * 判断系统
     */
    getxitong:function() {
	    var u = navigator.userAgent, app = navigator.appVersion;
	    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;
	    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	    if (isAndroid) {
	       return "android";
	    }
	    if (isIOS) {
		   return "ios";
	    }
	},
	/*
	 * 查看路径后面的参数（编码优化版）
	 */
	getrequest:function() {
		var url = location.search; //获取url中"?"符后的字串  
		var theRequest = new Object();
		if(url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for(var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(decodeURI(strs[i].split("=")[1]));
			}
		}
		return theRequest;
	},
	/*
	 * 图片压缩
	 */
	getBase64Image:function(imgurl,fun) {
		//mui.toast("开始压缩图片");
		//return new Promise((res, rej) => {
			var img = new Image();
			img.src = imgurl;
			var div = document.createElement('div');
			div.appendChild(img);
			img.onload = function() {
				var canvas = document.createElement("canvas");
				var imgw = img.width / 1.5;
				var imgh = img.height / 1.5;
				canvas.width = imgw;
				canvas.height = imgh;
				var ctx = canvas.getContext("2d");
				ctx.drawImage(img, 0, 0, imgw, imgh);
				var ext = img.src.substring(img.src.lastIndexOf(".") + 1).toLowerCase();
				var dataURL = canvas.toDataURL("image/" + ext);
				console.log(dataURL);
				canvas.toBlob(function(blob) {
					if(blob.size > 1024 * 1024) {
						this.getBase64Image(dataURL);
					} else {
						var lsit = {
							dataURL: dataURL,
							type: "image/" + ext
						}
						fun(lsit);
					}
				})
			}
		//})
	},
	/*
	 * base64转文件
	 */
	dataURLtoFile:function(dataurl, filename) {
		return new Promise((res, rej) => {
			var arr = dataurl.split(',');
			var mime = arr[0].match(/:(.*?);/)[1];
			var bstr = atob(arr[1]);
			var n = bstr.length;
			var u8arr = new Uint8Array(n);
			while(n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			console.log(u8arr);
			var newfile = new File([u8arr], filename, {
				type: mime
			});
			res(newfile);
		})
	},
	/*
	 * 验证非法字符
	 */
	checkStr:function(strTest) {
	     var pat = new RegExp("[^a-zA-Z0-9\_\u4e00-\u9fa5]", "i");
	     var strArr = strTest.split("");
	     for (var i = 0; i < strArr.length; i++) {
	         var str = strTest.charAt(i);
	         if (pat.test(strArr[i]) == true) {
	             strArr[i] = "()";
	             console.log("含有非法字符");
	             return false
	         }
	     }
	     return true
	},  
	//原生js文件上传
	upladfile:function(files, func) {
		let xhr = null,
			url = hurl + '/api/Image/upload',
			form = new FormData();
		form.append("image", files); // 文件对象
		form.append("access_token", acctoken()); // 可以增加表单数据
		xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
		xhr.open("post", url, true); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
		xhr.onload = function(evt) {
			var data = JSON.parse(evt.target.responseText);
			func(data);
		}
		xhr.onerror = function() { //请求失败 
			mui.toast("文件上传失败");
		}; 
		xhr.send(form); //开始上传，发送form数据
	},
	getInfo:function() { 
		var s = "";  
		s += " 网页可见区域宽："+ document.body.clientWidth;  
		s += " 网页可见区域高："+ document.body.clientHeight;  
		s += " 网页可见区域宽："+ document.body.offsetWidth + " (包括边线和滚动条的宽)";  
		s += " 网页可见区域高："+ document.body.offsetHeight + " (包括边线的宽)";  
		s += " 网页正文全文宽："+ document.body.scrollWidth; 
		s += " 网页正文全文高："+ document.body.scrollHeight;  
		s += " 网页被卷去的高(ff)："+ document.body.scrollTop;  
		s += " 网页被卷去的高(ie)："+ document.documentElement.scrollTop; 
		s += " 网页被卷去的左："+ document.body.scrollLeft;  
		s += " 网页正文部分上："+ window.screenTop;  
		s += " 网页正文部分左："+ window.screenLeft;  
		s += " 屏幕分辨率的高："+ window.screen.height;  s += " 屏幕分辨率的宽："+ window.screen.width; 
		s += " 屏幕可用工作区高度："+ window.screen.availHeight;
		s += " 屏幕可用工作区宽度："+ window.screen.availWidth;  
		s += " 你的屏幕设置是 "+ window.screen.colorDepth +" 位彩色"; 
		s += " 你的屏幕设置 "+ window.screen.deviceXDPI +" 像素/英寸"; 
		console.log(s);
	},
	
}
Array.prototype.removeindex = function(val) {  this.splice(val, 1) }; 
Array.prototype.removeval = function(val) {  var index = this.indexOf(val);  if (index > -1) {  this.splice(index, 1);  }  }; 
Array.prototype.indexOf = function(val) {  for (var i = 0; i < this.length; i++) {  if (this[i] == val) return i;  }  return -1;  }; 
